package com.yanqu.road.server.manger.cricket.skillhandler.attround.handler;

import com.yanqu.road.entity.cricket.helper.AddHpResult;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.gameplayer.module.cricket.battle.CricketGame;
import com.yanqu.road.server.gameplayer.module.cricket.battle.eCricketBattleRoundStep;
import com.yanqu.road.server.gameplayer.module.cricket.model.BattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.SkillData;
import com.yanqu.road.server.manger.cricket.skillhandler.attround.CricketSkillAttRoundStrategy;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class CricketSkill146Strategy implements CricketSkillAttRoundStrategy {

    /**
     * 技能处理-反弹受到攻击伤害的{0}。
     * @param cricketGame 本局对局对象
     * @param skillData 触发的技能
     * @param baseAttSkill 基础攻击技能
     * @param baseAttReHpResult 基础攻击结果对象
     * @param action 我方
     * @param target 对方
     * @param attackRoundBuilder 返回PB结构体
     * @return true：死亡，退出技能处理，false：继续技能处理
     */
    @Override
    public boolean skillHandle(CricketGame cricketGame, SkillData skillData, SkillData baseAttSkill, AddHpResult baseAttReHpResult, BattleData action, BattleData target, CricketProto.CricketAttackRound.Builder attackRoundBuilder, long battleFrameId) {
        //基础攻击miss，不触发技能
        if (baseAttReHpResult == null) {
            return false;
        }
        long attack = Math.abs(baseAttReHpResult.getValue());
        //反弹受到攻击伤害的{0}。
        skillData.effectAmountInc();
        CricketProto.CricketBattleFrame.Builder newBattleFrameBuilder = cricketGame.newBattleFrameBuilder();
        newBattleFrameBuilder.setFromBattleFrameId(battleFrameId);
        newBattleFrameBuilder.setActionUserId(target.getUserId());
        newBattleFrameBuilder.setTargetUserId(action.getUserId());
        newBattleFrameBuilder.setSkillId(skillData.getSkillId());
        //反弹的伤害
        BigDecimal value = BigDecimal.valueOf(-attack);
        value = value.multiply(BigDecimal.valueOf(skillData.getSkillEffect().getP1())).divide(BigDecimal.valueOf(1000), 0, RoundingMode.DOWN);
        AddHpResult addHpResult = action.addHp(value.longValue());
        newBattleFrameBuilder.setAttack(addHpResult.getValue());
        newBattleFrameBuilder.setIsKill(addHpResult.isKill());
        newBattleFrameBuilder.setHarmEffectSkillId(addHpResult.getReduceHpEffectSkillId());
        newBattleFrameBuilder.setIsMiss(addHpResult.isMissFlag());
        cricketGame.fillRemainHp(newBattleFrameBuilder);
        attackRoundBuilder.addAfterAttack(newBattleFrameBuilder);
        if (cricketGame.isOver(attackRoundBuilder, eCricketBattleRoundStep.AfterAttack.getValue(), newBattleFrameBuilder.getBattleFrameId())) {
            return true;
        }
        return false;
    }
}
